<template>
	<view class="module-block">  

		<view class="discounts-info" v-if="discountConfig">
			<view class="title">优惠信息</view>
			<view class="group">
				<view class="group__item" v-if="myapp.coupon"  @click="selectCoupon">
					<view class="left">
						<view class="label"><text class="iconfont coupon">&#xe644;</text>优惠券</view>
						<view class="value" v-if="discountInfo.coupon.id">{{discountInfo.coupon.name}}</view>
					</view>
					<view class="number">
						<text v-if="discountInfo.coupon.num">{{discountInfo.coupon.id?'-'+decimalMoney(discountInfo.coupon.money)+'元':(discountInfo.coupon.num+'张可用')}}</text>
						<text class="disabled" v-else>无可用</text>
						
						<text class="iconfont">&#xe67f;</text>
					</view>
				</view>
				<view class="group__item" v-if="myapp.redbag" @click="selectEnvelope">
					<view class="left">
						<view class="label"><text class="iconfont">&#xe649;</text>红包抵扣</view>
					</view>
					<view class="number">
						<block v-if="discountInfo.red_envelope.id && discountInfo.red_envelope.money > 0">
							-{{decimalMoney(discountInfo.red_envelope.money)}}元
						</block>
						<block v-else>
							<text v-if="envelopeList.length">{{envelopeList.length}}个可用</text>
							<text class="disabled" v-else>无可用</text>
						</block>
						<text class="iconfont">&#xe67f;</text>
					</view>
				</view>
				<view class="group__item" v-if="integralInfo.isopen == 1">
					<view class="integral">
						<view class="integral-top">
							<view class="use">使用<input class="input-text" type="number" v-model="discountInfo.integral.pubneed" @focus="checkIntegral" @blur="checkIntegral" @input="changeIntegral" placeholder="0" placeholder-class="input-placeholder">积分</view>
							<view class="deduction" v-if="discountInfo.integral.need>0 && discountInfo.integral.money>0">
								<text class="num">-{{globalExchangeRate}}{{decimalMoney(discountInfo.integral.money)}}</text>
							</view>
						</view>
						<view class="integral-valid">共<text class="num">{{integralInfo.userIntegral}}</text>积分，最多可使用<text class="num">{{integralInfo.toplimit}}</text>积分抵扣<text class="num">{{globalExchangeRate}}{{integralInfo.jifentprice}}</text></view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	/**
	 * 优惠信息
	 * 
	 */
	export default {
		name: 'stDiscountsInfo',
		props:{
			discountConfig: {
				type: Boolean,
				default: true
			},
			myapp:{
				type: Object,
				default(){
					return {}
				}
			},
			discountInfo:{
				type: Object,
				default(){
					return {}
				}
			},
			integralInfo:{
				type: Object,
				default(){
					return {}
				}
			},
			envelopeList:{
				type: Array,
				default(){
					return []
				}
			},
		},
		data() {
			return {
				
			};
		},
		methods: {
			// 积分输入
			changeIntegral(){
				this.integralCheck();
				this.integralUpdate()
			},
			// 金额保留两位
			decimalMoney(val){
				return parseFloat(val).toFixed(2);
			},
			// 积分输入验证
			integralCheck(isreturn = false){
				let toplimit  = parseInt(this.integralInfo.toplimit);
				let userjifen = parseInt(this.integralInfo.userIntegral);
				let max_jifen = toplimit > userjifen ? userjifen : toplimit;
				let pubneed = parseInt(this.discountInfo.integral.pubneed);
				
				let status=false;
				if(!pubneed)
				{
				    status = true;
				}
				else if(pubneed <= max_jifen)
				{
				    status = true;
				}
				
				// 添加积分不足提示
				let tips = '超过抵扣上限';
				if(pubneed > userjifen)
				{
					tips = '积分不足';
				}
							
				if(!isreturn && !status)
				{
					
				    uni.showToast({
				    	title: tips,
				    	icon: 'none'
				    });
				    this.integralPriceReset();
				}			
				return status;
			},
			checkIntegral(){
				if(!parseInt(this.discountInfo.integral.pubneed)){
					this.discountInfo.integral.pubneed = '';
				}
				if(parseInt(this.discountInfo.integral.pubneed) && parseInt(this.discountInfo.integral.pubneed) < this.integralInfo.cfg_exchange_jifen)
				{
				    uni.showToast({
				    	title: `抵扣积分不可低于${this.integralInfo.cfg_exchange_jifen}积分`,
				    	icon: 'none'
				    });
				    this.integralPriceReset();
				}
			},
			//积分刷新
			integralUpdate()
			{
				let money = this.integralCalculate();
				let pubneed = this.discountInfo.integral.pubneed;
				let result = this.integralCheck(true);
				let need = 0;
				if(result)
				{
				   need = pubneed;
				}
				// 更新积分数据
				pubneed = parseInt(pubneed);
				need = parseInt(need);
				this.$emit('integralUpdate',pubneed,need,money);
			},
			// 重置积分
			integralPriceReset() 
			{
				this.discountInfo.integral.need = 0;
				this.discountInfo.integral.money = 0;
				this.discountInfo.integral.pubneed = 0;
				// 积分刷新
				this.integralUpdate();
			},
			//积分抵现计算
			integralCalculate()
			{
				let toplimit  = parseInt(this.integralInfo.toplimit);
				let userIntegral = parseInt(this.integralInfo.userIntegral);
				let max_jifen = toplimit > userIntegral ? userIntegral : toplimit;
				let exchange = parseInt(this.integralInfo.cfg_exchange_jifen);
				if (!max_jifen || !exchange)
				  return 0;
				let needjifen = parseInt(this.discountInfo.integral.pubneed);
				if (!needjifen || needjifen <= 0)
				  return 0;
				let price = needjifen / exchange;
				  return price.toFixed(2);
			},
			// 优惠券操作
			selectCoupon(){
				this.$emit('selectCoupon');
			},
			// 选择红包
			selectEnvelope(){
				this.$emit('selectEnvelope');
			}
		}
	}
</script>

<style lang="scss" scoped>
	.module-block{
		padding: 0 24rpx;
		margin-bottom: 20rpx;
	}
	.discounts-info{
		padding: 28rpx 24rpx 0;
		border-radius: 20rpx;
		background-color: $uni-bg-color;
		margin-bottom: 20rpx;
		.title{
			line-height: 1.24;
			font-size: 34rpx;
			font-weight: bold;
		}
		.group{
			padding: 18rpx 0 0;
		}
		.group__item{
			@extend .justifyspacec;
			padding: 22rpx 0;
			font-size: $uni-font-size-base;
		}
		.left{
			@extend .displayflex;
		}
		.label{
			width: 184rpx;
			.iconfont{
				color: #fb560a;
				margin-right: 8rpx;
			}
		}
		.value{
			width: 252rpx;
			@extend .ellipse;
		}
		.number{
			@extend .alignItems;
			font-size: $uni-font-size-base;
			color: #ff4a50;
			.disabled{
				color: #aaa;
			}
			.iconfont{
				margin-left: 4rpx;
				color: #a0a0a0;
			}
		}
		.integral{
			display: block;
			width: 100%;
			.integral-top{
				@extend .justifyspacec;
				.use{
					@extend .alignItems;
					.input-text{
						display: inline-block;
						width: 92rpx;
						margin: 0 6rpx;
						font-size: $uni-font-size-base;
						text-align: center;
						border-bottom: 1rpx solid #e5e5e5;
					}
				}
				.deduction{
					font-family: Arial, Helvetica, sans-serif;
					.num{
						color: #ff4a50;
					}
				}
			}
			.integral-valid{
				margin-top: 8rpx;
				font-size: $uni-font-size-mini;
				color: $uni-text-color-grey;
				line-height: 2.2;
				font-family: Arial, Helvetica, sans-serif;
				.num{
					color: #ff4a50;
				}
			}
		}
	}
</style>
